package edu.uwm.cs552;

import java.awt.BorderLayout;
import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PlayerStatusPanel extends JPanel implements Observer{

	private static DecimalFormat moneyFormat = new DecimalFormat("M$ #.00");
	private static String formatMoney(double m) {
		return "Money: " + moneyFormat.format(m) + " ";
	}
	
	private static String formatTime(int t) {
		return " Time: " + t;
	}

	private JLabel money = new JLabel(formatMoney(0));
	private JLabel time = new JLabel(formatTime(10));
	private final JPanel moneyTimePanel = new JPanel();
	private final JPanel statusPanel = new JPanel();
	private CardStatusPanel cardPanel;;
	private TrainStatusPanel loadPanel;
	private Player player;
	private TrainStatus train;
	private CardStatus cards;
	private GoalCard selectedCard;
	private Goal selectedGoal;
	
	public PlayerStatusPanel(Player p, TrainStatus t, CardStatus c) {
		player = p;
		train = t;
		cards = c;
		
		player.setCards(cards);
		setLayout(new BorderLayout(0,10));
		cardPanel = new CardStatusPanel(cards);
		loadPanel = new TrainStatusPanel(train);
		statusPanel.setLayout(new BorderLayout(0,10));
		statusPanel.add(loadPanel,BorderLayout.NORTH);
		statusPanel.add(cardPanel,BorderLayout.CENTER);
		moneyTimePanel.setLayout(new BorderLayout());
		moneyTimePanel.setLayout(new BoxLayout(moneyTimePanel,BoxLayout.X_AXIS));
		moneyTimePanel.add(Box.createHorizontalGlue());
		moneyTimePanel.add(money);
		moneyTimePanel.add(time);
		moneyTimePanel.add(Box.createHorizontalGlue());
		add(moneyTimePanel,BorderLayout.NORTH);
		add(statusPanel,BorderLayout.CENTER);
		money.setText(formatMoney(player.getMoney()));

		player.addObserver(new Observer() {
			public void update(Observable o, Object arg) {
				if (o == player) {
					if (arg instanceof Double) money.setText(formatMoney(player.getMoney()));
					else time.setText(formatTime(player.getTime()));
				}
			}
		});
	}
	
	@Override
	public void update(Observable o, Object arg) {
		
	}

}
